<template>
      <div class="app-container">
            <p>{{hospitalSet===''?'医院设置添加':'医院设置修改'}}</p>
              <el-form label-width="120px">
                     <el-form-item label="医院名称">
                <el-input v-model="hospitalSet.hosname" />
            </el-form-item>
                     <el-form-item label="医院编号">
                <el-input v-model="hospitalSet.hoscode" />
            </el-form-item>
                     <el-form-item label="api基础路径">
                <el-input v-model="hospitalSet.apiUrl" />
            </el-form-item>
                     <el-form-item label="联系人姓名">
                <el-input v-model="hospitalSet.contactsName" />
            </el-form-item>
                     <el-form-item label="联系人手机">
                <el-input v-model="hospitalSet.contactsPhone" />
            </el-form-item>
                     <el-form-item>
                            <el-button type="primary" @click="saveOrUpdate">保存</el-button>
                      </el-form-item>
                  </el-form>
           </div>
</template>

<script>
    import { addHospSet, getHospSetById, updateHospSet } from '@/api/hospSet'
    export default {
        data() {
            return {
                hospitalSet: {},
            }
        },
        created() {
            this.init()
        },
        watch: {
            $route(to, from) {
            this.init();
            },
        },
        methods: {
            init() {
                if (this.$route.params && this.$route.params.id) {
                    const id = this.$route.params.id
                    //通过id查找
                    getHospSetById(id).then(res => {
                        this.hospitalSet = res.data
                    })
                } else {
                    //添加之前清空表格
                    this.hospitalSet = {}
                }
            },
            //添加
            save() {
                addHospSet(this.hospitalSet).then(res => {
                    this.$router.push({ path: '/hospSet/list' })
                    this.$message({
                        type: "success",
                        message: "添加成功"
                    });
                }).catch(err => {
                    this.$message({
                        type: "error",
                        message: "添加失败"
                    });
                })
            },
            //修改
            update() {
                updateHospSet(this.hospitalSet).then(res => {
                    this.$router.push({ path: '/hospSet/list' })
                    this.$message({
                        type: "success",
                        message: "修改成功"
                    });
                }).catch(err => {
                    this.$message({
                        type: "error",
                        message: "修改失败"
                    });
                })
            },
            saveOrUpdate() {
                //判断添加还是修改
                if (this.hospitalSet.id) {
                    this.update()
                } else {
                    this.save()
                }
            }
        }
    }
</script>